package com.zdft.promotion.promotion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdft.promotion.promotion.bean.SingleProductBean;
import com.zdft.promotion.promotion.bean.SingleProductDetailsBean;
import com.zdft.promotion.promotion.mapper.SingleProductDetailsMapper;
import com.zdft.promotion.promotion.mapper.SingleProductMapper;
import com.zdft.promotion.promotion.service.api.ISingleProductDetailsService;
import com.zdft.promotion.promotion.service.api.ISingleProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SingleProductServiceImpl extends ServiceImpl<SingleProductMapper, SingleProductBean> implements ISingleProductService {

    @Autowired
    SingleProductMapper singleProductMapper;
    @Autowired
    SingleProductDetailsMapper singleProductDetailsMapper;
    @Autowired
    ISingleProductDetailsService singleProductDetailsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        // 删除单品分档
        singleProductMapper.deleteById(id);
        // 删除分档明细
        QueryWrapper<SingleProductDetailsBean> query = new QueryWrapper();
        query.lambda().eq(SingleProductDetailsBean::getSingleProductId, id);
        singleProductDetailsMapper.delete(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(SingleProductBean bean, List<SingleProductDetailsBean> details) {
        // 处理单品分档
        saveOrUpdate(bean);
        // 批量处理分档明细
        singleProductDetailsService.saveOrUpdateBatch(details);
    }
}